﻿namespace EyeSoft.Demo.FiscalCode.Wpf.ViewModels
{
	using System;

	using EyeSoft.FluentValidation;

	using global::FluentValidation;

	public class FiscalCodeViewModelValidator : FluentValidator<FiscalCodeViewModel>
	{
		public FiscalCodeViewModelValidator()
		{
			RuleFor(x => x.FirstName).NotNull().Length(3, 20).OnlyCharacters();
			RuleFor(x => x.LastName).NotNull().Length(3, 30).OnlyCharacters();

			RuleFor(x => x.TownSearch).Must(TownSearchNotEmptyAndValid).WithMessage("Town is not valid.");

			RuleFor(x => x.Birthdate).GreaterThan(new DateTime(1900, 1, 1));
		}

		private bool TownSearchNotEmptyAndValid(FiscalCodeViewModel viewModel, string townSearch)
		{
			var valid = string.IsNullOrWhiteSpace(townSearch) || viewModel.Town != null;

			return valid;
		}
	}
}